import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show SystemUiOverlayStyle;

class GKBBackButton extends StatelessWidget {
  final VoidCallback? onTap;
  const GKBBackButton({Key? key, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Navigator.of(context).canPop()
        ? IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: onTap ??
                () {
                  Navigator.of(context).pop();
                },
          )
        : const SizedBox();
  }
}

class GKBAppBar {
  static AppBar gradient(
    BuildContext context, {
    required List<Color> colors,
    String? titleText,
    Widget? title,
    SystemUiOverlayStyle? brightness,
    Color? iconColor,
    bool centerTitle = false,
    TextStyle? titleStyle,
    List<Widget>? actions,
    VoidCallback? onBack,
  }) {
    return AppBar(
      title: title ??
          DefaultTextStyle(
              style: Theme.of(context).appBarTheme.titleTextStyle!,
              child: Text(
                titleText ?? '',
                style: titleStyle,
              )),
      systemOverlayStyle: brightness,
      elevation: 0,
      centerTitle: centerTitle,
      iconTheme: IconThemeData(
        color: iconColor,
      ),
      actions: actions,
      leading: GKBBackButton(
        onTap: onBack,
      ),
      flexibleSpace: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: colors,
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter),
        ),
      ),
    );
  }

  static AppBar create(
    BuildContext context, {
    required String title,
    SystemUiOverlayStyle? brightness,
    Color? backgroundColor,
    Color? iconColor,
    bool centerTitle = false,
    TextStyle? titleStyle,
    List<Widget>? actions,
    VoidCallback? onBack,
  }) {
    return AppBar(
      title: DefaultTextStyle(
          style: Theme.of(context).appBarTheme.titleTextStyle!,
          child: Text(
            title,
            style: titleStyle,
          )),
      systemOverlayStyle: brightness,
      backgroundColor: backgroundColor,
      elevation: 0,
      centerTitle: centerTitle,
      iconTheme: IconThemeData(
        color: iconColor,
      ),
      actions: actions,
      leading: GKBBackButton(
        onTap: onBack,
      ),
    );
  }

  static AppBar close(
    BuildContext context,
  ) {
    return AppBar(
      backgroundColor: Colors.transparent,
      elevation: 0,
      //actions: [],
      leading: IconButton(
        color: Colors.white,
        onPressed: () {
          Navigator.of(context).pop();
        },
        icon: Container(
            decoration: BoxDecoration(
                color: Colors.black12, borderRadius: BorderRadius.circular(15)),
            child: const Icon(Icons.close)),
      ),
      systemOverlayStyle: SystemUiOverlayStyle.light,
    );
  }
}
